package io.gitee.zhangbinhub.acp.cloud.server.helloworld;

import io.gitee.zhangbinhub.acp.cloud.resource.server.annotation.AcpCloudResourceServerApplication;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.web.client.ClientHttpRequestFactories;
import org.springframework.boot.web.client.ClientHttpRequestFactorySettings;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.openfeign.support.FeignHttpClientProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestClient;

import java.time.Duration;

/**
 * @since JDK 17
 */
@AcpCloudResourceServerApplication
public class HelloWorldApplication {

    public static void main(String[] args) {
        SpringApplication.run(HelloWorldApplication.class, args);
    }

    @Bean("customerRestClientTest")
    @LoadBalanced
    public RestClient.Builder restClient(FeignHttpClientProperties feignHttpClientProperties) {
        ClientHttpRequestFactorySettings clientHttpRequestFactorySettings = ClientHttpRequestFactorySettings.DEFAULTS;
        return RestClient.builder()
                .requestFactory(
                        ClientHttpRequestFactories.get(clientHttpRequestFactorySettings
                                .withConnectTimeout(
                                        Duration.of(
                                                feignHttpClientProperties.getHc5().getConnectionRequestTimeout(),
                                                feignHttpClientProperties.getHc5().getConnectionRequestTimeoutUnit().toChronoUnit()
                                        ))
                                .withReadTimeout(
                                        Duration.of(
                                                feignHttpClientProperties.getHc5().getSocketTimeout(),
                                                feignHttpClientProperties.getHc5().getSocketTimeoutUnit().toChronoUnit()
                                        ))
                        )
                );
    }

}
